#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=1e6+10;
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b); 
}
int main()
{
	vector<int> p;
	int t;
	cin>>t;
	while(t--)
	{
		int a,b;
		scanf("%d %d",&a,&b);
		int n=2;
		while(true)
		{
			int bb=b*n;
			n++;
			if(bb>a)
			{
				if(gcd(bb,a)==b)
				{
					printf("%d\n",bb);
					break;
				}
			}
			else
			{
				if(gcd(a,bb)==b)
				{
					printf("%d\n",bb);
					break;
				}
			}
		}
	}

	return 0;
}

